package com.liauto.timelist.ui.time

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.Animation
import android.view.animation.RotateAnimation
import androidx.lifecycle.ViewModelProvider
import com.liauto.timelist.R
import com.liauto.timelist.databinding.FragmentTimeBinding
import com.liauto.timelist.ui.BaseFragment


class TimeFragment : BaseFragment<FragmentTimeBinding, TimeViewModel>() {

    override fun provideViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): FragmentTimeBinding {
        return FragmentTimeBinding.inflate(inflater, container, false)
    }

    override fun provideViewModel(): TimeViewModel {
        return ViewModelProvider(this)[TimeViewModel::class.java]
    }

    override fun initEvent() {
        arguments?.getInt("hour")?.let {
            rotatePoint((it % 12) * 30f, binding.hourPoint)
        }
        arguments?.getInt("min")?.let {
            rotatePoint(it * 6f, binding.minutePoint)
        }
        binding.bg.setOnClickListener {
            backToListFragment()
        }
    }

    private fun rotatePoint(toDegrees: Float, view: View) {
        RotateAnimation(
            0f,
            toDegrees,
            Animation.RELATIVE_TO_SELF,
            0.5f,
            Animation.RELATIVE_TO_SELF,
            0.5f
        ).apply {
            fillAfter = true
            duration = 500
            startOffset = resources.getInteger(R.integer.card_flip_time_full).toLong()
            interpolator = AccelerateDecelerateInterpolator()
            view.startAnimation(this)
        }
    }
}